package sddz.foodmanage.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.List;

import sddz.foodmanage.R;
import sddz.foodmanage.model.CommodityBean;
import sddz.foodmanage.model.InpurchaingBean;
import sddz.foodmanage.utils.ImageLoader;

/**
 * @Date:2019/7/13
 * @Author:lbb
 * 采购中
 */

public class InpurchaingAdapter extends BaseAdapter {
    private Context context;
    private List<InpurchaingBean.DataBean.GoodsBean> list;
    private ModifyCountInterface modifyCountInterface;
    private int userid;

    //商品id
    private int carid;
    //商品数量
    private String num;

    //==============================================================================================
    //用于给每个订单加备注
    private List<String> goods_remark_list = new ArrayList<>();
    //==============================================================================================


    public InpurchaingAdapter(){

    }
    public void setdata(Context context,List<InpurchaingBean.DataBean.GoodsBean> list,List<String> remark){
        this.context=context;
        this.list=list;
        goods_remark_list=remark;
    }
    public InpurchaingAdapter(Context context,List<InpurchaingBean.DataBean.GoodsBean> list){
        this.context=context;
        this.list=list;
    }
    public void setModifyCountInterface(ModifyCountInterface modifyCountInterface){
        this.modifyCountInterface=modifyCountInterface;
        if(this.modifyCountInterface==null){
            Log.e("modifyCountInterface","为null");
        }else{
            Log.e("modifyCountInterface","不为空");
        }
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        InpurchaingBean.DataBean.GoodsBean dataBean= (InpurchaingBean.DataBean.GoodsBean) getItem(position);
        if(convertView == null){
            LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.item_home_shangpin, null);
            holder = new ViewHolder();
            holder.spname = convertView.findViewById(R.id.tv_spname);
            holder.price = convertView.findViewById(R.id.tv_unitprice);
            holder.img = convertView.findViewById(R.id.image_sp);
            holder.dsx = convertView.findViewById(R.id.image_dsx);
            holder.weight = convertView.findViewById(R.id.tv_weight);
            holder.jia = convertView.findViewById(R.id.image_add);
            holder.jian = convertView.findViewById(R.id.image_jian);
            holder.num = convertView.findViewById(R.id.rl_num);
            holder.changesc = convertView.findViewById(R.id.rl_changesc);
            holder.note = convertView.findViewById(R.id.arl_note);
            holder.note.setVisibility(View.VISIBLE);
            holder.delect = convertView.findViewById(R.id.arl_delect);
            holder.imdelect = convertView.findViewById(R.id.im_delect);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        final InpurchaingBean.DataBean.GoodsBean info = list.get(position);
        if(info != null){

            holder.spname.setText(list.get(position).getName());
            holder.price.setText(list.get(position).getPrice() + "元/" + list.get(position).getUnit());
            holder.weight.setText(list.get(position).getAmount()+"");
            if(list.get(position).getImgs().size()>0) {
                ImageLoader.loadImageUrl(context, list.get(position).getImgs().get(0).getUrl(), holder.img);
            }
                holder.num.setVisibility(View.VISIBLE);
                holder.delect.setVisibility(View.VISIBLE);
                holder.changesc.setVisibility(View.GONE);
            holder.dsx.setVisibility(View.GONE);

            holder.jia.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    modifyCountInterface.doIncrease( holder.weight,position);// 暴露增加接口
                }
            });


            holder.jian.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    modifyCountInterface.doDecrease( holder.weight,position);// 暴露删减接口
                }
            });
            holder.weight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    modifyCountInterface.doTextView(holder.weight,position);
                }
            });
            holder.imdelect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    modifyCountInterface.doDelect(holder.imdelect,position);
                }
            });
            //======================================================================================
            //添加备注信息
            holder.note.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    /*@setView 装入一个EditView
                     */
                    final EditText editText = new EditText(context);
                    AlertDialog.Builder inputDialog =
                            new AlertDialog.Builder(context);
                    inputDialog.setTitle("请输入备注信息").setView(editText);
//                    inputDialog.setCanceledOnTouchOutside(false);
                    inputDialog.setCancelable(true);
                    editText.setText(goods_remark_list.get(position));
                    inputDialog.setNegativeButton("取消",new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                        }
                    });
                    inputDialog.setPositiveButton("确定",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    goods_remark_list.set(position,editText.getText().toString());
                                    Toast.makeText(context, "添加备注成功", Toast.LENGTH_SHORT).show();
                                }
                            })
                            ;
                    inputDialog.show();
                }
            });
            //======================================================================================
        }

        return convertView;
    }



    class ViewHolder{
        ImageView img,dsx;
        ImageView jia,jian,imdelect;
        TextView spname,price;
        TextView weight;
        AutoRelativeLayout num,changesc,note,delect;
    }

    /**
     * 改变数量的接口
     */
    public interface ModifyCountInterface {
        /**
         * 增加操作
         *
         *
         * @param showCountView 用于展示变化后数量的View
         */
        public void doIncrease(View showCountView,int position);

        /**
         * 删减操作
         *
         *
         * @param showCountView 用于展示变化后数量的View
         */
        public void doDecrease(View showCountView,int position);
        public void doTextView(View showCountView,int position);
        public void doDelect(View showCountView,int position);

    }
}
